package com.rowenci.iot_agriculture.service.impl;

import com.rowenci.iot_agriculture.bean.iot.Message;
import com.rowenci.iot_agriculture.mapper.MessageDao;
import com.rowenci.iot_agriculture.service.api.MessageService;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 *
 * </p>
 *
 * @author rowenci
 * @date 2019/4/16 20:03
 */
@Service
public class MessageServiceImpl implements MessageService {

    @Resource
    private MessageDao messageDao;

    @Override

    public Message findMessage(Integer comId) {
        return messageDao.findByComId(comId);
    }

    /**
     * 当注入不成功时，使用JDBC来重写Dao方法
     * 判断条件为 messageDao == null
     * @param message
     */
    @Override
    public void updateMessage(Message message) {
        if (messageDao == null){
            messageDao = new MessageDao() {
                @Override
                public Message findByComId(Integer comId) {
                    return null;
                }

                @Override
                public void updateMessage(String info, Integer comId) {
                    String driver = "com.mysql.cj.jdbc.Driver";
                    String url = "jdbc:mysql://cdb-qhgx4ekn.gz.tencentcdb.com:10000/iot_agriculture?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true";
                    String username = "root";
                    String password = "bb3960519";
                    Connection conn = null;
                    try {
                        Class.forName(driver); //classLoader,加载对应驱动
                        conn = (Connection) DriverManager.getConnection(url, username, password);
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }

                    int i = 0;
                    String sql = "update message set info='" + info + "' where com_id='" + comId + "'";
                    PreparedStatement pstmt;
                    try {
                        pstmt = (PreparedStatement) conn.prepareStatement(sql);
                        i = pstmt.executeUpdate();
                        System.out.println("resutl: " + i);
                        pstmt.close();
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public List<Message> findAll() {
                    return null;
                }

                @Override
                public List<Message> findAll(Sort sort) {
                    return null;
                }

                @Override
                public List<Message> findAllById(Iterable<Integer> iterable) {
                    return null;
                }

                @Override
                public <S extends Message> List<S> saveAll(Iterable<S> iterable) {
                    return null;
                }

                @Override
                public void flush() {

                }

                @Override
                public <S extends Message> S saveAndFlush(S s) {
                    return null;
                }

                @Override
                public void deleteInBatch(Iterable<Message> iterable) {

                }

                @Override
                public void deleteAllInBatch() {

                }

                @Override
                public Message getOne(Integer integer) {
                    return null;
                }

                @Override
                public <S extends Message> List<S> findAll(Example<S> example) {
                    return null;
                }

                @Override
                public <S extends Message> List<S> findAll(Example<S> example, Sort sort) {
                    return null;
                }

                @Override
                public Page<Message> findAll(Pageable pageable) {
                    return null;
                }

                @Override
                public <S extends Message> S save(S s) {
                    return null;
                }

                @Override
                public Optional<Message> findById(Integer integer) {
                    return Optional.empty();
                }

                @Override
                public boolean existsById(Integer integer) {
                    return false;
                }

                @Override
                public long count() {
                    return 0;
                }

                @Override
                public void deleteById(Integer integer) {

                }

                @Override
                public void delete(Message message) {

                }

                @Override
                public void deleteAll(Iterable<? extends Message> iterable) {

                }

                @Override
                public void deleteAll() {

                }

                @Override
                public <S extends Message> Optional<S> findOne(Example<S> example) {
                    return Optional.empty();
                }

                @Override
                public <S extends Message> Page<S> findAll(Example<S> example, Pageable pageable) {
                    return null;
                }

                @Override
                public <S extends Message> long count(Example<S> example) {
                    return 0;
                }

                @Override
                public <S extends Message> boolean exists(Example<S> example) {
                    return false;
                }
            };
        }
        messageDao.updateMessage(message.getInfo(), message.getComId());
    }

    @Override
    public void addMessage(Message message) {
        messageDao.save(message);
    }

}
